<?php

namespace App\Http\Controllers\WorkOrder\Web;

use App\Http\Controllers\BaseController;
use App\Service\WorkOrder\Web\WorkflowV2TypeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class WorkflowV2TypeController extends BaseController
{
    /**
     * 获得工单分类列表
     */
    public function list(Request $request): JsonResponse
    {
        $data = WorkflowV2TypeService::list();

        return $this->success($data);
    }

    /**
     * 添加工单分类
     * @throws ValidationException
     */
    public function save(Request $request): JsonResponse
    {
        $this->validate($request, [
            'name'        => 'required|string',
            'prefix'      => 'required|string',
            'icon'        => 'required|string',
            'description' => 'required|string',
            'sort'        => 'required|int',
        ]);

        $id          = (int)$request->post('id', 0);
        $name        = (string)$request->post('name');
        $prefix      = (string)$request->post('prefix');
        $description = (string)$request->post('description');
        $icon        = (string)$request->post('icon');
        $sort        = (int)$request->post('sort');

        WorkflowV2TypeService::save($id, $name, $prefix, $description, $icon, $sort);

        return $this->success();
    }

    /**
     * 删除分类
     */
    public function delete(Request $request, $id): JsonResponse
    {

        WorkflowV2TypeService::delete($id);

        return $this->success();
    }

    /**
     * 修改状态
     * @throws ValidationException
     */
    public function updateStatus(Request $request, $id): JsonResponse
    {
        $this->validate($request, [
            'status' => 'required|int'
        ]);

        $status = (int)$request->post('status');

        WorkflowV2TypeService::updateStatus($id, $status);

        return $this->success();
    }


}
